package com.scalar.chiptrack.finance.openpo.actioncommands;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.ScalarException;
import com.scalar.chiptrack.ChipTrackExtendedAction;
import com.scalar.chiptrack.commons.ClientInfo;
import com.scalar.chiptrack.finance.openpo.dao.PurchaseOrderDAO;
import com.scalar.chiptrack.finance.openpo.forms.PurchaseOrderForm;
import com.scalar.chiptrack.operations.vendor.Vendor;
import com.scalar.chiptrack.operations.vendor.dao.VendorDAO;
import com.scalar.chiptrack.sales.dao.SalesDAO;
import com.scalar.chiptrack.users.ErrorReport;
import com.scalar.chiptrack.users.dao.UserDAO;
import com.scalar.chiptrack.utils.Logger;

/**
 * Created by KUMARASWAMY.
 * Date: Feb 24, 2005
 * Time: 10:20:00 PM
 */

/**
 *  PurchaseOrderPrinterFriendlyCommand processess the request from the Purchase Order screen to Printer Friendly page
 *
 *  @author KumaraSwamy
 *  @version
 */

public class StandardPurchaseOrderPrinterFriendlyCommand extends ChipTrackExtendedAction
{
     /**
     * Loads the Purchase Order Information matching the given criteria from the database
     * @throws com.scalar.ScalarException
     */

    public ActionForward executeRequest( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws ScalarException
    {
        HashMap errors			= ( HashMap ) request.getAttribute( ERROR_MESSAGES );
		HashMap info            = ( HashMap ) request.getAttribute( INFO_MESSAGES );
		String actionForward 	= "printerFriendly";
		String action   		= request.getParameter( REQUEST_ACTION );
        String purchaseOrderId  = request.getParameter ( "purchaseOrderId");
        String vendor_id        = request.getParameter ( "vendorId");
        int    vendorId         = Integer.parseInt( vendor_id );

		HttpSession mSession 	= request.getSession();

		PurchaseOrderForm purchaseOrderForm = ( PurchaseOrderForm ) form;

		try
		{
			if( action.equalsIgnoreCase( "printerFriendly" ) )
			{
                try
                {
                    purchaseOrderForm.setPurchaseOrder_Id( Integer.parseInt( purchaseOrderId) );
                }
                catch( Exception e)
                {
                    purchaseOrderForm.setPurchaseOrder_Id(0);
                }
                try
                {
                    PurchaseOrderDAO.loadPurchaseOrder( purchaseOrderForm) ;
                    ClientInfo clientInfo = UserDAO.getClientAddressInfo();
                    purchaseOrderForm.setClientInfo( clientInfo);

                    purchaseOrderForm.setBillingAddress( PurchaseOrderDAO.getClientAddress() );
			        purchaseOrderForm.setShippingAddress( PurchaseOrderDAO.getAddress( purchaseOrderForm.getShippingAddress_id() ) );

                    Vendor vendor          = VendorDAO.getVendorDetails( vendorId );
                    purchaseOrderForm.setVendorName( vendor.getVendorName() );
                }
                catch( ScalarException se)
                {
                    Logger.error( ERROR_LOADING_PURCHASE_ORDER_INFORMATION, se );
                    errors.put( ERROR_INTERNAL_SERVER_ERROR, ERROR_LOADING_PURCHASE_ORDER_INFORMATION );
                    se.printStackTrace();
                }               
				request.setAttribute ( "financeReasonCodes", SalesDAO.loadFinanceReasonCodes () );
				request.setAttribute ( "opsReasonCodes", SalesDAO.loadOpsReasonCodes () );
				actionForward = "printerFriendly";
			}
		}
		catch ( Exception e )
		{
			Logger.error( ERROR_LOADING_OPEN_PURCHASE_ORDER_INFORMATION, e );
			errors.put( ERROR_INTERNAL_SERVER_ERROR, ERROR_LOADING_OPEN_PURCHASE_ORDER_INFORMATION );
			mSession.setAttribute( ERROR_REPORT_OBJECT, new ErrorReport( "Error while executing the command", e ) );
			actionForward = "Generate";
		}
		request.setAttribute( ERROR_MESSAGES, errors );
		request.setAttribute( INFO_MESSAGES, info );
		return mapping.findForward( actionForward );
    }
}
